﻿/******************************************************************************
文件名称    :   MySqlChemistryUrineDataProvider.cs
创建日期    :   2011/5/19 21:25:43
功能描述    :   化验尿常实体数据提供程序MySql实现类
修改日志    :   
    2011-05-19 21:25  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// 化验尿常实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlChemistryUrineDataProvider : ChemistryUrineDataProvider
    {
        public MySqlChemistryUrineDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, ChemistryUrine entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_urine(weight, ph, wbc, nitrite, protein, rbcul, rbchp, wbcul, wbchp, tcul, tchp, tube, crystal, comment) values(?weight, ?ph, ?wbc, ?nitrite, ?protein, ?rbcul, ?rbchp, ?wbcul, ?wbchp, ?tcul, ?tchp, ?tube, ?crystal, ?comment);" +
                    "select id, time_stamp from tb_chemistry_urine where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?weight", DbType.Decimal).Value = entity.Weight == null ? DBNull.Value : (object)entity.Weight.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ph", DbType.Decimal).Value = entity.Ph == null ? DBNull.Value : (object)entity.Ph.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?wbc", DbType.Decimal).Value = entity.Wbc == null ? DBNull.Value : (object)entity.Wbc.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?nitrite", DbType.Decimal).Value = entity.Nitrite == null ? DBNull.Value : (object)entity.Nitrite.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?protein", DbType.Decimal).Value = entity.Protein == null ? DBNull.Value : (object)entity.Protein.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?rbcul", DbType.Decimal).Value = entity.Rbcul == null ? DBNull.Value : (object)entity.Rbcul.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?rbchp", DbType.Decimal).Value = entity.Rbchp == null ? DBNull.Value : (object)entity.Rbchp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?wbcul", DbType.Decimal).Value = entity.Wbcul == null ? DBNull.Value : (object)entity.Wbcul.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?wbchp", DbType.Decimal).Value = entity.Wbchp == null ? DBNull.Value : (object)entity.Wbchp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?tcul", DbType.Decimal).Value = entity.Tcul == null ? DBNull.Value : (object)entity.Tcul.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?tchp", DbType.Decimal).Value = entity.Tchp == null ? DBNull.Value : (object)entity.Tchp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?tube", DbType.Decimal).Value = entity.Tube == null ? DBNull.Value : (object)entity.Tube.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?crystal", DbType.Decimal).Value = entity.Crystal == null ? DBNull.Value : (object)entity.Crystal.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<ChemistryUrine> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_urine(id, weight, ph, wbc, nitrite, protein, rbcul, rbchp, wbcul, wbchp, tcul, tchp, tube, crystal, comment, time_stamp) values(?id, ?weight, ?ph, ?wbc, ?nitrite, ?protein, ?rbcul, ?rbchp, ?wbcul, ?wbchp, ?tcul, ?tchp, ?tube, ?crystal, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?weight", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?ph", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?wbc", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?nitrite", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?protein", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?rbcul", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?rbchp", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?wbcul", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?wbchp", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?tcul", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?tchp", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?tube", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?crystal", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (ChemistryUrine entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.Weight == null ? DBNull.Value : (object)entity.Weight.Value;
                    cmd.Parameters[i++].Value = entity.Ph == null ? DBNull.Value : (object)entity.Ph.Value;
                    cmd.Parameters[i++].Value = entity.Wbc == null ? DBNull.Value : (object)entity.Wbc.Value;
                    cmd.Parameters[i++].Value = entity.Nitrite == null ? DBNull.Value : (object)entity.Nitrite.Value;
                    cmd.Parameters[i++].Value = entity.Protein == null ? DBNull.Value : (object)entity.Protein.Value;
                    cmd.Parameters[i++].Value = entity.Rbcul == null ? DBNull.Value : (object)entity.Rbcul.Value;
                    cmd.Parameters[i++].Value = entity.Rbchp == null ? DBNull.Value : (object)entity.Rbchp.Value;
                    cmd.Parameters[i++].Value = entity.Wbcul == null ? DBNull.Value : (object)entity.Wbcul.Value;
                    cmd.Parameters[i++].Value = entity.Wbchp == null ? DBNull.Value : (object)entity.Wbchp.Value;
                    cmd.Parameters[i++].Value = entity.Tcul == null ? DBNull.Value : (object)entity.Tcul.Value;
                    cmd.Parameters[i++].Value = entity.Tchp == null ? DBNull.Value : (object)entity.Tchp.Value;
                    cmd.Parameters[i++].Value = entity.Tube == null ? DBNull.Value : (object)entity.Tube.Value;
                    cmd.Parameters[i++].Value = entity.Crystal == null ? DBNull.Value : (object)entity.Crystal.Value;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, ChemistryUrine entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_chemistry_urine set weight = ?weight, ph = ?ph, wbc = ?wbc, nitrite = ?nitrite, protein = ?protein, rbcul = ?rbcul, rbchp = ?rbchp, wbcul = ?wbcul, wbchp = ?wbchp, tcul = ?tcul, tchp = ?tchp, tube = ?tube, crystal = ?crystal, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_chemistry_urine where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?weight", DbType.Decimal).Value = entity.Weight == null ? DBNull.Value : (object)entity.Weight.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ph", DbType.Decimal).Value = entity.Ph == null ? DBNull.Value : (object)entity.Ph.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?wbc", DbType.Decimal).Value = entity.Wbc == null ? DBNull.Value : (object)entity.Wbc.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?nitrite", DbType.Decimal).Value = entity.Nitrite == null ? DBNull.Value : (object)entity.Nitrite.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?protein", DbType.Decimal).Value = entity.Protein == null ? DBNull.Value : (object)entity.Protein.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?rbcul", DbType.Decimal).Value = entity.Rbcul == null ? DBNull.Value : (object)entity.Rbcul.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?rbchp", DbType.Decimal).Value = entity.Rbchp == null ? DBNull.Value : (object)entity.Rbchp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?wbcul", DbType.Decimal).Value = entity.Wbcul == null ? DBNull.Value : (object)entity.Wbcul.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?wbchp", DbType.Decimal).Value = entity.Wbchp == null ? DBNull.Value : (object)entity.Wbchp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?tcul", DbType.Decimal).Value = entity.Tcul == null ? DBNull.Value : (object)entity.Tcul.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?tchp", DbType.Decimal).Value = entity.Tchp == null ? DBNull.Value : (object)entity.Tchp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?tube", DbType.Decimal).Value = entity.Tube == null ? DBNull.Value : (object)entity.Tube.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?crystal", DbType.Decimal).Value = entity.Crystal == null ? DBNull.Value : (object)entity.Crystal.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_chemistry_urine where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_chemistry_urine {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override ChemistryUrine Get(TransactionManager manager, long key)
        {
            ChemistryUrine entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_chemistry_urine where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<ChemistryUrine> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<ChemistryUrine> list = new List<ChemistryUrine>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_chemistry_urine {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_chemistry_urine {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_chemistry_urine {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override ChemistryUrine Fill(IDataReader reader, ChemistryUrine entity)
        {
            if (entity == null) entity = new ChemistryUrine();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "weight":
                        entity.Weight = reader[i] == DBNull.Value ? entity.Weight : reader.GetDecimal(i);
                        break;
                    case "ph":
                        entity.Ph = reader[i] == DBNull.Value ? entity.Ph : reader.GetDecimal(i);
                        break;
                    case "wbc":
                        entity.Wbc = reader[i] == DBNull.Value ? entity.Wbc : reader.GetDecimal(i);
                        break;
                    case "nitrite":
                        entity.Nitrite = reader[i] == DBNull.Value ? entity.Nitrite : reader.GetDecimal(i);
                        break;
                    case "protein":
                        entity.Protein = reader[i] == DBNull.Value ? entity.Protein : reader.GetDecimal(i);
                        break;
                    case "rbcul":
                        entity.Rbcul = reader[i] == DBNull.Value ? entity.Rbcul : reader.GetDecimal(i);
                        break;
                    case "rbchp":
                        entity.Rbchp = reader[i] == DBNull.Value ? entity.Rbchp : reader.GetDecimal(i);
                        break;
                    case "wbcul":
                        entity.Wbcul = reader[i] == DBNull.Value ? entity.Wbcul : reader.GetDecimal(i);
                        break;
                    case "wbchp":
                        entity.Wbchp = reader[i] == DBNull.Value ? entity.Wbchp : reader.GetDecimal(i);
                        break;
                    case "tcul":
                        entity.Tcul = reader[i] == DBNull.Value ? entity.Tcul : reader.GetDecimal(i);
                        break;
                    case "tchp":
                        entity.Tchp = reader[i] == DBNull.Value ? entity.Tchp : reader.GetDecimal(i);
                        break;
                    case "tube":
                        entity.Tube = reader[i] == DBNull.Value ? entity.Tube : reader.GetDecimal(i);
                        break;
                    case "crystal":
                        entity.Crystal = reader[i] == DBNull.Value ? entity.Crystal : reader.GetDecimal(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion
    }
}
